#if defined(__linux__)
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include <signal.h>
#include <iostream>
#include <frpc/frpc.h>
#include <protocol/frpc_msgio.h>
#include "workflow/WFServer.h"
#include "workflow/Workflow.h"
#include "workflow/WFFacilities.h"

#include <handler/handlers.h>
#include <task/frpc_task.h>
#include <cppbase/datetime_type.h>
using task_cb_t = std::function<void(WFNetworkTask<Frpc::ReplyMsg,Frpc::ReplyMsg> *)>;
		


std::string host = "127.0.0.1";
int port = 8080;



void callback(WFNetworkTask<Frpc::RequestMsg, Frpc::ReplyMsg> *task)
{

	int state = task->get_state();
	int error = task->get_error();
	Frpc::ReplyMsg *resp = task->get_resp();

	if (state != WFT_STATE_SUCCESS)
	{
		if (state == WFT_STATE_SYS_ERROR)
			fprintf(stderr, "SYS error: %s\n", strerror(error));
		else if (state == WFT_STATE_DNS_ERROR)
			fprintf(stderr, "DNS error: %s\n", gai_strerror(error));
		else
			fprintf(stderr, "other error.\n");
		return;
	}

	std::cout << "recieved content length:" << resp->recieced_contentsize() << std::endl;
	DLOG("row size:%d,row count:%d",*(uint16_t*)resp->input().data(),*(uint16_t*)(resp->input().data()+2));
	DLOG("first day:%d",*(uint32_t*)(resp->input().data()+4+99*6244));
/*
		Frpc::ContractArray ca;
		auto res = ca.ParseFromArray(resp->input().data(), resp->recieced_contentsize());
		if (!res)
			printf("failed to parse content\n");
		printf("cnt:%d\n", ca.contract_size());
		for (int i = 0; i < ca.contract_size() && i < 30; i++)
		{
			auto c = ca.contract(i);
			printf("%ld,%s,%s,%d\n", c.id(), c.name().data(), c.symbol().data(), c.exchange_id());
		}
*/
		return;



}
int main(int argc, char *argv[])
{
	/* First request is emtpy. We will ignore the server response. */
	WFFacilities::WaitGroup wait_group(1);
	auto *task = new Frpc::Task(callback);

	Frpc::GetBarRequest bar;
	bar.set_exchange_id(1);
	bar.set_symbol("000001");
	bar.set_barsize(1);
	bar.set_rbegin(0);
	bar.set_count(100);
	//bar.set_start_date(DateTime::fromUint32(20210601).time());
	bar.set_end_date(DateTime::fromUint32(20210801).time());
	auto s=bar.SerializeAsString();
	task->get_req()->set_cmd_output(Command::GetContractBar, std::move(s));

	Workflow::start_series_work(task, [&wait_group](const SeriesWork *)
								{
		wait_group.done(); });
								

	wait_group.wait();
	return 0;
}